<?php
require_once(_PS_MODULE_DIR_.'prestatheme/models/ThemeEditor.php');
class AdminThemeEditorController extends ModuleAdminController
{
	public function __construct()
	{
		$this->table      = 'pt_theme_editor';
		$this->identifier = 'id_editor';
		$this->className  = 'ThemeEditor';
		
		$this->context = Context::getContext();
		
		$this->addRowAction('edit');
		$this->addRowAction('delete');
		$this->addRowAction('add');
        
        //var_dump($this->title,$this->show_toolbar,$this->toolbar_scroll,$this->toolbar_btn);die();
		
		$this->bulk_actions = array(
			'delete' => array(
				'text' => $this->l('Delete selected'), 
				'confirm' => $this->l('Delete selected items?')
			)
		);

		$this->fields_list = array(
			'id_editor' => array(
				'title' => $this->l('ID'), 
				'align' => 'center', 
				'width' => 25
			),
			'cms' => array(
				'title' => $this->l('CMS'), 
				'align' => 'left', 
			),
            'date_add' => array(
                'title' 	=> $this->l('Created On'),
                'type' 	=> 'date',
				'width' => 150
            ),
            'date_upd' => array(
                'title' 	=> $this->l('Last update On'),
                'type' 	=> 'date',
				'width' => 150
            ),
			'active' => array(
				'title' => $this->l('Enabled'), 
				'width' => 25, 
				'align' => 'center', 
				'active' => 'status', 
				'type' => 'bool', 
				'orderby' => false
			)
		);
		
        $this->_join = '
		LEFT JOIN `'._DB_PREFIX_.'pt_cms` c ON (c.`id_cms` = a.`id_cms`)';
		$this->_select = 'CONCAT(c.`code`, \' \', c.`version`) AS cms';
        
		parent::__construct();
	}
    
	public function initToolbar()
	{
		parent::initToolbar();
        $this->toolbar_scroll = false;
        
		$back = self::$currentIndex.'&token='.$this->token;
		$this->toolbar_btn['back'] = array(
			'href' => $back,
			'desc' => $this->l('Back to editors')
		);
	}
	
	public function renderForm()
	{
		$this->fields_form = array(
			'tinymce' => true,
			'legend' => array(
				'title' => $this->l('Prestatheme editors interface'),
				'image' => '../img/admin/cog.gif'
			),
			'input' => array(
				array(
					'type' => 'select',
					'label' => $this->l('Prestatheme CMS'),
					'name' => 'id_cms',
					'required' => true,
					'options' => array(
						'query' => PrestathemeCms::getPrestathemeCMS(),
						'id' => 'id_cms',
						'name' => 'fullname'
					)
				),
				array(
					'type' => 'radio',
					'label' => $this->l('Available'),
					'name' => 'active',
					'required' => false,
					'class' => 't',
					'is_bool' => true,
					'values' => array(
						array(
							'id' => 'active_on',
							'value' => 1,
							'label' => $this->l('Enabled')
						),
						array(
							'id' => 'active_off',
							'value' => 0,
							'label' => $this->l('Disabled')
						)
					),
				),
			),
			'submit' => array(
				'title' => $this->l('   Save   '),
				'class' => 'button'
			)
		);

		if (!($obj = $this->loadObject(true)))
			return;

		return parent::renderForm();
	}

	public function renderView()
	{
		if (!($object = $this->loadObject()))
			return;
			
		$this->tpl_view_vars = array(
			'id_lang' => Context::getContext()->language->id,
			'object' => $object,
			'head_title' => ($object->id ? $this->l('Editor for') .': '. $object->getCMSName() : $this->l('New Prestatheme editor interface')),
			'shopContext' => Shop::getContext(),
		);

		return parent::renderView();
	}
}